#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

int mysystem(char *command)
{
    int status;
    pid_t pid;

    if (command == NULL)
        return 1;

    if ((pid = fork()) < 0)
        status = -1;
    else if  (pid == 0)
    {
        execlp("/bin/sh", "sh", "-c", command, NULL);
        _exit(127);
    }
    else if (pid > 0)
    {
        if ((waitpid(pid, &status, 0) < 0) && errno != EINTR)
            status = -1;
    }

    return status;
}

int main(int argc, char *argvp[])
{
    int ret;

    if (argc < 2)
    {
        printf("usage %s <exec>\n", argv[0]);
        exit(0);
    }

    ret = mysystem(argv[1]);
    printf("return of the mysystem is %d\n", ret);

    return 0;
}
